package customer.zspt.cn.customer.components.chat.ChatOrderContent.OrderElementPg;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import customer.zspt.cn.customer.R;

/**
 * Created by Administrator on 2018/8/16 0016.
 */

public class CheckBoxView extends BaseView {
    private  TextView title;
    private  TextView choose_Tv;
    String result;


    private JSONArray options;
    private boolean[] selectedStates;
    private String[] optionsStr;
    /**
     *
     * @param context
     * @param titleVaules 标题
     * @param options 选择项
     * @param selected 已选择的项
     */
    public CheckBoxView(final Context context, String titleVaules, JSONArray options, JSONArray selected) {
        super(context,titleVaules);
        View.inflate(context, R.layout.chat_order_content_checkbox,this);
        this.options = options;
        selectedStates = new boolean[options.length()];
        optionsStr = new String[options.length()];
        String defaultValues = "";
        for(int i=0;i<options.length();i++){
            try {
                JSONObject object = (JSONObject) options.get(i);
                optionsStr[i] = object.getString("label");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        for(int i=0;i<selected.length();i++){
            try {
                defaultValues+=i<selected.length()-1?selected.get(i).toString()+",":
                        selected.get(i).toString();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        for(int i=0;i<selected.length();i++){
            for(int j=0;j<options.length();j++){
                try{
                    String label = (String) options.getJSONObject(j).get("label");
                    if(selected.get(i).toString().equals(label)){
                        selectedStates[j] = true;
                    }
                }catch (Exception e){
                     e.printStackTrace();
                }
            }
        }
        choose_Tv = (TextView) findViewById(R.id.choose_Tv);
        title = (TextView) findViewById(R.id.title);
        title.setText(titleVaules);
        choose_Tv.setText(defaultValues);
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(context);
            }
        });
    }
    /**弹出时间选择框*/
    private void showDialog(Context context) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle("多项选项选择");
        dialog.setMultiChoiceItems(optionsStr, selectedStates, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                 if(isChecked){
                     selectedStates[which] = true;
                 }else{
                     selectedStates[which] = false;
                 }
            }
        });
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                result = "";
                for(int i=0;i<selectedStates.length;i++){
                    if(selectedStates[i]){
                        result+=i<selectedStates.length-1?optionsStr[i]+",":optionsStr[i];
                    }
                }
                choose_Tv.setText(result+">");
            }
        });
        dialog.show();
    }
    @Override
    public String result() {
        return result;
    }
}
